package com.yida.gwj.common.enums;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.Date;
import java.util.Objects;

/**
 * mq延迟等级枚举 1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h
 *
 * @author lgp
 * @date 2022/08/24
 */

public enum MqDelayLevelEnum {
    /**
     * 需要修改/conf/broker.conf配置文件
     * messageDelayLevel = 1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h
     */
    ZERO(0, "0s"),
    ONE(1, "1s"),
    TWO(2, "5s"),
    THREE(3, "10s"),
    FOUR(4, "30s"),
    FIVE(5, "1m"),
    SIX(6, "2m"),
    SEVEN(7, "3m"),
    EIGHT(8, "4m"),
    NINE(9, "5m"),
    TEN(10, "6m"),
    ELEVEN(11, "7m"),
    TWELVE(12, "8m"),
    THIRTEEN(13, "9m"),
    FOURTEEN(14, "10m"),
    FIFTEEN(15, "20m"),
    SIXTEEN(16, "30m"),
    SEVENTEEN(17, "1h"),
    EIGHTEEN(18, "2h");

    private final Integer code;
    private final String msg;


    public static MqDelayLevelEnum judgeLevel(LocalDateTime localDateTime, MqDelayLevelEnum ignoreLevelEum) {
        if (localDateTime.isBefore(LocalDateTime.now())) {
            return MqDelayLevelEnum.ONE;
        }
        Date sendTime = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        for (int i = values().length; i > 0; i--) {
            int finalI = i;
            MqDelayLevelEnum levelEnum = Arrays.stream(values()).filter(level -> level.getCode() == finalI)
                    .findFirst().orElseThrow(() -> new NullPointerException("MQ等级匹配失败"));
            String msg = levelEnum.getMsg();
            String time = getTime(msg);
            if (msg.contains("h")) {
                if (Objects.nonNull(ignoreLevelEum)) {
                    if (ignoreLevelEum.getMsg().contains("h")) {
                        return ignoreLevelEum;
                    }
                }
                Date offsetTime = DateUtil.offsetHour(new Date(), Integer.parseInt(time));
                if (sendTime.after(offsetTime)) {
                    return levelEnum;
                }
            } else if (msg.contains("m")) {
                if (Objects.nonNull(ignoreLevelEum)) {
                    if (ignoreLevelEum.getMsg().contains("m")) {
                        return ignoreLevelEum;
                    }
                }
                Date offsetTime = DateUtil.offsetMinute(new Date(), Integer.parseInt(time));
                if (sendTime.after(offsetTime)) {
                    return levelEnum;
                }
            } else if (msg.contains("s")) {
                if (Objects.nonNull(ignoreLevelEum)) {
                    if (ignoreLevelEum.getMsg().contains("s")) {
                        return ignoreLevelEum;
                    }
                }
                Date offsetTime = DateUtil.offsetSecond(new Date(), Integer.parseInt(time));
                if (sendTime.after(offsetTime)) {
                    return levelEnum;
                }
            }
        }
        return MqDelayLevelEnum.ONE;
    }

    public static String getTime(String msg) {
        return StrUtil.sub(msg, -1, 0);
    }

    public Integer getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    MqDelayLevelEnum(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }
}
